1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: ClearHelper.java,v 1.2 2004/02/03 07:31:02 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.message.clear;
46
47 import javax.jms.BytesMessage;
48 import javax.jms.Message;
49 import javax.jms.MessageEOFException;
50 import javax.jms.MessageNotReadableException;
51 import javax.jms.StreamMessage;
52
53 import org.apache.log4j.Category;
54
55 import junit.framework.Assert;
56
57 import org.exolab.jmscts.core.MessageVerifier;
58 import org.exolab.jmscts.test.message.util.EmptyMessageVerifier;
59 import org.exolab.jmscts.test.message.util.EmptyPropertyVerifier;
60
61
62 /***
63 * This class provides helper methods for testing the behaviour of
64 * {@link Message#clearBody} and {@link Message#clearProperties}
65 *
66 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
67 * @version $Revision: 1.2 $
68 */
69 class ClearHelper extends Assert {
70
71 /***
72 * The logger
73 */
74 private static final Category log =
75 Category.getInstance(ClearHelper.class);
76
77
78 /***
79 * Check that invoking {@link Message#clearProperties} leaves a message
80 * with no properties, but does not affect the message body
81 *
82 * @param message the message to check
83 * @param verifier the verifier to check that the body hasn't changed
84 * @param provider true if the message may contain provider properties
85 * @throws Exception for any error
86 */
87 public static void checkClearProperties(
88 Message message, MessageVerifier verifier, boolean provider)
89 throws Exception {
90
91 try {
92 message.clearProperties();
93 } catch (Exception exception) {
94 String msg = "Message.clearProperties() failed";
95 log.debug(msg, exception);
96 fail(msg + ": " + exception);
97 }
98
99 try {
100 MessageVerifier empty = new EmptyPropertyVerifier(provider);
101 empty.verify(message);
102 } catch (Exception exception) {
103 String msg = "Expected clearProperties() to clear all "
104 + "properties";
105 log.debug(msg, exception);
106 fail(msg);
107 }
108
109 // need to reset BytesMessage and StreamMessage instances to make
110 // them readable
111 if (message instanceof BytesMessage) {
112 ((BytesMessage) message).reset();
113 } else if (message instanceof StreamMessage) {
114 ((StreamMessage) message).reset();
115 }
116
117 try {
118 verifier.verify(message);
119 } catch (Exception exception) {
120 String msg = "Message body should not have changed after "
121 + "invoking Message.clearProperties() : " + exception;
122 log.debug(msg, exception);
123 fail(msg);
124 }
125 }
126
127 /***
128 * Verify that a message has no body, and that the message properties
129 * haven't changed
130 *
131 * @param message the message to check
132 * @param verifier the verifier to check that the properties haven't
133 * changed
134 * @throws Exception for any error
135 */
136 public static void checkClearBody(Message message,
137 MessageVerifier verifier)
138 throws Exception {
139
140 try {
141 message.clearBody();
142 } catch (Exception exception) {
143 String msg = "Message.clearBody() failed";
144 log.debug(msg, exception);
145 fail(msg + ": " + exception);
146 }
147
148 try {
149 verifier.verify(message);
150 } catch (Exception exception) {
151 String msg = "Message properties should not have changed after "
152 + "invoking Message.clearBody()";
153 log.debug(msg, exception);
154 fail(msg + ": " + exception);
155 }
156
157 MessageVerifier empty = null;
158 if (message instanceof BytesMessage
159 || message instanceof StreamMessage) {
160
161 // check that invoking methods on BytesMessage or StreamMessage
162 // throws MessageNotReadableException after clearBody has been
163 // invoked
164 empty = new EmptyMessageVerifier(
165 MessageNotReadableException.class);
166 empty.verify(message);
167
168 // now reset them to make them readable
169 if (message instanceof BytesMessage) {
170 ((BytesMessage) message).reset();
171 } else if (message instanceof StreamMessage) {
172 ((StreamMessage) message).reset();
173 }
174 }
175
176 empty = createEmptyVerifier(message);
177
178 try {
179 empty.verify(message);
180 } catch (Exception exception) {
181 String msg = "Message body should not have any content "
182 + "after invoking clearBody()";
183 log.debug(msg, exception);
184 fail(msg + ": " + exception);
185 }
186 }
187
188 /***
189 * Create a new {@link EmptyMessageVerifier} for the supplied message
190 *
191 * @param message the message to create the verifier for
192 * @return a new message verifier
193 */
194 public static EmptyMessageVerifier createEmptyVerifier(Message message) {
195 EmptyMessageVerifier result = null;
196 if (message instanceof BytesMessage
197 || message instanceof StreamMessage) {
198 result = new EmptyMessageVerifier(MessageEOFException.class);
199 } else {
200 result = new EmptyMessageVerifier();
201 }
202 return result;
203 }
204
205 }
This page was automatically generated by Maven